package com.rtwo.tio.server;

import com.rtwo.tio.common.BasicPacket;
import com.rtwo.tio.common.MsgType;
import com.rtwo.tio.common.BasicHandler;
import com.rtwo.tio.common.TioHandler;
import com.rtwo.tio.common.heartbeat.HeartBeatHandler;
import com.rtwo.tio.server.handler.GroupMsgResponseHandler;
import com.rtwo.tio.server.handler.JoinGroupResponseHandler;
import com.rtwo.tio.server.handler.LoginResponseHandler;
import com.rtwo.tio.server.handler.P2PResponseHandler;
import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

import java.util.*;

/**
 * Created by hasee on 2018/7/4.
 */
public class ServerTioHandlerImp extends TioHandler implements ServerAioHandler{

    private static Map<Byte,BasicHandler<?>> handlers = new HashMap<>();

    static
    {
        handlers.put(MsgType.LOGIN_REQ,new LoginResponseHandler());
        handlers.put(MsgType.HEART_BEAT_REQ,new HeartBeatHandler());
        handlers.put(MsgType.P2P_REQ,new P2PResponseHandler());
        handlers.put(MsgType.GROUP_JOIN_REQ,new JoinGroupResponseHandler());
        handlers.put(MsgType.GROUP_MSG_REQ,new GroupMsgResponseHandler());
    }

    @Override
    public void handler(Packet packet, ChannelContext channelContext) throws Exception {
        BasicPacket basicPacket = (BasicPacket)packet;
        byte type = basicPacket.getType();
        BasicHandler<?> _handler = handlers.get(type);
        _handler.handler(packet,channelContext);
    }
}
